﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommandService
{
   public class PubMethods
    {
        /// <summary>
        /// 获取当前时间(yyyy-MM-dd HH:mm:ss)
        /// </summary>
        /// <returns></returns>
        public string getNow()
        {
            return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }
        /// <summary>
        /// 获取当前时间(yyyy-MM-dd)
        /// </summary>
        /// <returns></returns>
        public string getNowDate()
        {
            return DateTime.Now.ToString("yyyy-MM-dd");
        }
        /// <summary>
        /// 获取时间字符串(yyyyMMddHHmmss)
        /// </summary>
        /// <returns></returns>
        public string getTimeString()
        {
            return DateTime.Now.ToString("yyyyMMddHHmmss");
        }
       /// <summary>
       /// 查询字符串拼接
       /// </summary>
       /// <param name="sb"></param>
       /// <param name="condition"></param>
       /// <returns></returns>
        public StringBuilder CreateSelectCondition(StringBuilder sb, string condition)
        {
            if (!string.IsNullOrWhiteSpace(condition))
            {
                var json = JsonConvert.DeserializeObject<JObject>(condition);
                foreach (JProperty obj in json.Properties())
                {
                    if (!string.IsNullOrWhiteSpace(obj.Value.ToString()))
                    {
                        if (!string.IsNullOrWhiteSpace(sb.ToString()))
                        {
                            sb.Append(" and ");
                        }
                        sb.Append(obj.Name + " like '%" + obj.Value.ToString() + "%' ");
                    }
                }
            }
            return sb;
        }
    }
}
